Scroll Progress Bar

Constructor

In C++, a constructor is a special member function of a class that is automatically called when an object of the class is created. Constructors are used to initialize the data members (properties) of an object and perform any necessary setup. Constructors have the same name as the class and do not have a return type. Here are the key points about constructors in C++:

1. Default Constructor:
  • A default constructor is a constructor that takes no parameters.
  • If don't provide any constructors in class, C++ will provide a default constructor automatically.
  • It initializes data members to default values (e.g., 0 for numeric types, empty for strings).
Program:

class MyClass {
public:
    int myData;

    // Default constructor (provided by C++)
    MyClass() {
        myData = 0; // Initialize data member
    }
};
2. Parameterized Constructor:
  • A parameterized constructor is a constructor that takes one or more parameters.
  • It allows to initialize data members with specific values when creating an object.
Program:

class Person {
public:
    std::string name;
    int age;

    // Parameterized constructor
    Person(std::string n, int a) {
        name = n;
        age = a;
    }
};
3. Multiple Constructors (Overloading):

it can have multiple constructors in a class, each with a different number or type of parameters. This is called constructor overloading. Overloaded constructors provide flexibility for creating objects with different initializations.

Program:

class Rectangle {
public:
    int width;
    int height;

    // Default constructor
    Rectangle() {
        width = 0;
        height = 0;
    }

    // Parameterized constructor
    Rectangle(int w, int h) {
        width = w;
        height = h;
    }
};
4. Initialization Lists:

Constructor initialization lists allow to initialize data members directly when the object is created. They are more efficient than assigning values inside the constructor body.

Program:

class Circle {
private:
    double radius;

public:
    // Parameterized constructor with initialization list
    Circle(double r) : radius(r) {
        // Constructor body (if needed)
    }
};
5. Copy Constructor:

A copy constructor is a special constructor that creates a new object as a copy of an existing object of the same class. It is called when an object is passed by value, returned by value, or explicitly copied.

Program:

class MyString {
private:
    char* str;

public:
    // Copy constructor
    MyString(const MyString& other) {
        // Implement copying logic (deep copy)
    }
};
6. Destructor:
  • A destructor is a special member function with the same name as the class, preceded by a tilde (~).
  • It is called automatically when an object goes out of scope or is explicitly deleted.
  • Destructors are used to release resources (e.g., memory) held by the object.
Program:

class DynamicArray {
private:
    int* data;

public:
    // Constructor
    DynamicArray(int size) {
        data = new int[size];
    }

    // Destructor
    ~DynamicArray() {
        delete[] data; // Release dynamically allocated memory
    }
};

Constructors play a crucial role in C++ classes, allowing to set up objects with the correct initial state. By defining and using constructors appropriately, it can ensure that objects are initialized correctly and prevent undefined behavior.


question


answer

question2


answer2